import 'package:fluro/fluro.dart';
import 'package:flutter/cupertino.dart';
import './router_handler.dart';

///
/// fluro页面路由总配置
///
class Routes {
  static late FluroRouter router;
  static String home = '/';
  static String login = '/login';
  static String register = '/register';
  static String assetGeneralDetail = '/asset_general_detail';
  static String mineInfo = '/mine_info';
  static String mineSetting = '/mine_setting';
  static String mineAbout = '/mine_about';
  static String photoPreview = '/photo_preview';
  static String productDetail = '/product_detail';
  static String announcement = '/announcement';
  static String mineTeam = '/mine_team';
  static String assetGold = '/asset_gold';
  static String orderConfirm = '/order_confirm';
  static String orderCashier = '/order_cashier';
  static String mineShareCard = '/mine_share_card';
  static String mineAddressManager = '/mine_address_manager';
  static String mineAddressEdit = '/mine_address_edit';
  static String orderIndex = '/order_index';
  static String orderDetail = '/order_detail';
  static String productClassify = '/product_classify';
  static String productList = '/product_list';
  static String productSpecial = '/product_special';
  static String webView = '/web_view';
  static String mineCollcet = '/mine_collect';
  static String mineAddressChoose = '/mine_address_choose';
  static String assetPackge = '/asset_packge';
  static String mineChangeNickname = '/mine_change_nickname';
  static String mineChangePassword = '/mine_change_password';
  static String mineAccountSafety = '/mine_account_safety';
  static String mineAssetTransition = '/mine_asset_transition';
  static String mineAssetExchange = '/mine_asset_exchange';
  static String mallHome = '/mall_home';
  static String mineFindPassword = '/mine_find_password';
  static String mineChangeMobile = '/mine_change_mobile';

  static void initRouter() {
    router = FluroRouter();
    defineRoutes();
  }

  ///
  /// 路由的路径和动作一对一绑定
  ///
  static void defineRoutes() {
    router.define(home, handler: homePageHandler);
    router.define(login, handler: loginPageHandler);
    router.define(register, handler: registerPageHandler);
    router.define(assetGeneralDetail, handler: assetGeneralDtailPageHandler);
    router.define(mineInfo, handler: mineInfoHandler);
    router.define(mineSetting, handler: mineSettingHandler);
    router.define(mineAbout, handler: mineAboutHandler);
    router.define(photoPreview, handler: photoPreviewHandler);
    router.define(productDetail, handler: productDtailPageHandler);
    router.define(announcement, handler: announcementPageHandler);
    router.define(mineTeam, handler: mineTeamPageHandler);
    router.define(assetGold, handler: assetGoldPageHandler);
    router.define(orderConfirm, handler: orderConfirmPageHandler);
    router.define(orderCashier, handler: cashierPageHandler);
    router.define(mineShareCard, handler: mineShareCardHandler);
    router.define(mineAddressManager, handler: mineAddressManagerHandler);
    router.define(mineAddressEdit, handler: mineAddressEditHandler);
    router.define(orderIndex, handler: orderIndexPageHandler);
    router.define(orderDetail, handler: orderDetailHandler);
    router.define(productClassify, handler: productClassifyHandler);
    router.define(productList, handler: productListHandler);
    router.define(productSpecial, handler: productSpecialAreaHandler);
    router.define(webView, handler: webViewHandler);
    router.define(mineCollcet, handler: mineCollectHandler);
    router.define(mineAddressChoose, handler: mineAddressChooseHandler);
    router.define(assetPackge, handler: assetPackgePageHandler);
    router.define(mineChangeNickname, handler: mineChangeNicknameHandler);
    router.define(mineChangePassword, handler: mineChangePasswordHandler);
    router.define(mineAccountSafety, handler: mineAccountSafetyHandler);
    router.define(mineAssetTransition, handler: mineAssetTransitionHandler);
    router.define(mineAssetExchange, handler: mineAssetExchangeHandler);
    router.define(mallHome, handler: mallHomeHandler);
    router.define(mineFindPassword, handler: mineFindPasswordHandler);
    router.define(mineChangeMobile, handler: mineChangeMobileHandler);

    router.notFoundHandler = notFoundHandler;
  }

  // 对参数进行encode，解决参数中有特殊字符，影响fluro路由匹配
  static Future navigateTo(BuildContext context, String path,
      {Map<String, dynamic>? params,
        TransitionType transition = TransitionType.inFromRight}) {
    String query = "";
    if (params != null) {
      int index = 0;
      for (var key in params.keys) {
        var value = Uri.encodeComponent(params[key]);
        if (index == 0) {
          query = "?";
        } else {
          query = "$query&";
        }
        query += "$key=$value";
        index++;
      }
    }

    path = path + query;
    print('我是navigateTo跳转：$path');
    return router.navigateTo(context, path, transition: transition);
  }
}
